package com.iafme.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iafme.dto.req.CawReqDTO;
import com.iafme.dto.req.FileReqDTO;
import com.iafme.dto.req.UploadFileResDTO;
import com.iafme.dto.res.CawResDTO;
import com.iafme.mapper.CawMapper;
import com.iafme.model.Caw;
import com.iafme.model.CawFile;
import com.iafme.service.CawFileService;
import com.iafme.service.CawService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/18 19:46
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
@AllArgsConstructor
public class CawServiceImpl extends ServiceImpl<CawMapper, Caw> implements CawService {


    @Override
    public Page<CawResDTO> lists(CawReqDTO cawReqDTO) {
        Page<Caw> page=new Page<>(cawReqDTO.getPageNum(),cawReqDTO.getPageSize());
        LambdaQueryWrapper<Caw> like = Wrappers.lambdaQuery(Caw.class).
                like(StrUtil.isNotEmpty(cawReqDTO.getName()), Caw::getName, cawReqDTO.getName())
                .eq(Caw::getType,cawReqDTO.getType())
                .eq(ObjectUtil.isNotEmpty(cawReqDTO.getIsTopic()),Caw::getIsTopic,cawReqDTO.getIsTopic())
                .orderByDesc(Caw::getCreateTime);
        return Convert.convert(new TypeReference<Page<CawResDTO>>() {
        },this.page(page,like));
    }

    @Override
    public void saveCaw(CawReqDTO cawReqDTO) {
        Caw caw = Convert.convert(Caw.class, cawReqDTO);
        this.save(caw);
        saveCawFile(cawReqDTO.getFileList(),caw.getId());
    }

    @Override
    public void updateCaw(CawReqDTO cawReqDTO) {
        Caw caw = Convert.convert(Caw.class, cawReqDTO);
        this.updateById(caw);
        saveCawFile(cawReqDTO.getFileList(),caw.getId());
    }

    private final CawFileService cawFileService;

    private void saveCawFile(List<UploadFileResDTO<Map<String, Object>>> fileList,Integer cawId) {
        LambdaQueryWrapper<CawFile> eq = Wrappers.lambdaQuery(CawFile.class).eq(CawFile::getCawId, cawId);
        cawFileService.remove(eq);

        List<CawFile> cawFileList= ListUtil.list(true);
        for (UploadFileResDTO<Map<String, Object>> uploadFileResDTO : fileList) {
            Map<String, Object> response = uploadFileResDTO.getResponse();
            if(CollUtil.isNotEmpty(response) && response.get("code").equals(200)){
                String name = uploadFileResDTO.getName();
                String data = Convert.convert(String.class, response.get("data"));
                CawFile cawFile=new CawFile();
                cawFile.setName(name);
                cawFile.setUrl(data);
                cawFile.setCawId(cawId);
                cawFileList.add(cawFile);
            }
        }

        if(CollUtil.isNotEmpty(cawFileList) && cawFileList.size()>0){
            cawFileService.saveBatch(cawFileList);
        }


    }

    @Override
    public CawResDTO findById(Integer id) {
        List<UploadFileResDTO<Map<String,Object>>> fileList=ListUtil.list(true);
        CawResDTO cawResDTO = Convert.convert(CawResDTO.class,this.getById(id));
        LambdaQueryWrapper<CawFile> eq = Wrappers.lambdaQuery(CawFile.class).eq(CawFile::getCawId, id);
        List<CawFile> cawFileList = cawFileService.list(eq);
        cawFileList.forEach(cawFile -> {
            UploadFileResDTO<Map<String,Object>> upload=new UploadFileResDTO<>();
            upload.setName(cawFile.getName());
            upload.setUid(IdUtil.fastUUID());
            Map<String,Object> map= MapUtil.newHashMap();
            map.put("data",cawFile.getUrl());
            map.put("code",200);
            upload.setResponse(map);
            fileList.add(upload);

        });
        cawResDTO.setFileList(fileList);
        return cawResDTO;
    }
}
